package com.timothy.android.chart.view;

import java.util.List;

import com.timothy.android.chart.model.BaseBean;
import com.timothy.android.chart.model.ChoiceResult;
import com.timothy.android.chart.model.PageBaseBean;
import com.timothy.android.config.CanvasSetting;
import com.timothy.android.config.Configuration;
import com.timothy.android.config.NumberUtil;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;


public class Bar2DVerView extends SurfaceView implements SurfaceHolder.Callback{
	private final static String LOG_TAG = "Bar2DVerView";
	
	private SurfaceHolder mHolder;
//	private GestureDetector mGestureDetector = null;
	private Integer screenSize;

	private BaseBean baseBean;
	private Integer page;
	
	private final static float BAR_RATIO = 0.8f;
	private final static int CHOICE_COUNT_PER_PAGE = 8;
	
	public Bar2DVerView(Context context) {
		super(context);
		Log.i(LOG_TAG, "Bar2DVerView(Context context)...");
		mHolder = getHolder();
		mHolder.addCallback(this);// 设置Surface生命周期回调
	}
	
	public Bar2DVerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i(LOG_TAG, "Bar2DVerView(Context context, AttributeSet attrs)...");
		mHolder = getHolder();
		mHolder.addCallback(this);// 设置Surface生命周期回调
	}

	/*public Bar2DVerticalView(Context context,Integer screenSize,BaseBean baseBean, Integer page) {
		super(context);
		mHolder = getHolder();
		mHolder.addCallback(this);// 设置Surface生命周期回调
		this.screenSize = screenSize;
		this.baseBean = baseBean;
		this.page = page;
	}*/


	public void setScreenSize(Integer screenSize) {
		this.screenSize = screenSize;
	}

	public void setBaseBean(BaseBean baseBean) {
		this.baseBean = baseBean;
	}

	public void setPage(Integer page) {
		this.page = page;
	}
	
	public Integer getPage() {
		return page;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
		Log.i(LOG_TAG, "surfaceChanged()...");
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		Log.i(LOG_TAG, "surfaceCreated()...");
		DrawVertical2DBar(mHolder,baseBean,page,screenSize);
		Log.i(LOG_TAG, "surfaceCreated() END.");
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.i(LOG_TAG, "surfaceDestroyed()...");
	}
	
	public void DrawVertical2DBar(SurfaceHolder sfh,BaseBean baseBean,Integer page,Integer screenSize ){
		Log.i(LOG_TAG, "DrawVertical2DBar()...");
		Log.i(LOG_TAG, "page:"+page);
		
		Canvas canvas = sfh.lockCanvas();
		int canvasHeight = canvas.getHeight();
		int canvasWidth = canvas.getWidth();
		Log.i(LOG_TAG, "Canvas Height:"+canvasHeight);
		Log.i(LOG_TAG, "Canvas Width:"+canvasWidth);
		
		CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,screenSize);
		
		int leftM = setting.getLeftMargin();
		int rightM = setting.getRightMargin();
		int topM = setting.getTopMargin();
		int bottomM = setting.getBottomMargin();
		
		Log.i(LOG_TAG, "leftM:"+leftM);
		Log.i(LOG_TAG, "rightM:"+rightM);
		Log.i(LOG_TAG, "topM:"+topM);
		Log.i(LOG_TAG, "bottomM:"+bottomM);
		
		//set canvas background color
		canvas.drawColor(setting.getBgColor());
		Paint mPaint = new Paint();

		//write question
		mPaint.setColor(Color.parseColor(CanvasSetting.CHART_QUESTION_TEXT_COLOR));		
		mPaint.setTextSize(setting.getQueTextSize());
		Log.i(LOG_TAG, "CHART_QUESTION_TEXT_COLOR:"+CanvasSetting.CHART_QUESTION_TEXT_COLOR);
		Log.i(LOG_TAG, "CHART_QUESTION_TEXT_SIZE:"+setting.getQueTextSize());
		
		int queX = setting.getLeftMargin();
		int queY = setting.getTopMargin();
		Log.i(LOG_TAG, "queX:"+queX);
		Log.i(LOG_TAG, "queY:"+queY);
		
		//--------------if type=42, this is for priority question type---------------- 
		String questionString = baseBean.getQuestionText();
		/*if("42".equals(type)&&priority!=null){
			questionString = questionString + "(priority = "+priority+")";
		}*/
		//----------------------------------------------------------------------------
		
		
		//---------------------draw question start-------------------
		Log.i(LOG_TAG, "questionString:"+questionString);
		int queLength = questionString.length();
		Log.i(LOG_TAG, "queLength:"+queLength);
		int line = 0;
		int lineHeight = 0;
		if(queLength>CanvasSetting.QUESTION_WRAP_LENGTH_VERTIAL){
			Log.i(LOG_TAG, "draw question text->queLength>"+CanvasSetting.QUESTION_WRAP_LENGTH_VERTIAL);
			for(line = 0;line<queLength/CanvasSetting.QUESTION_WRAP_LENGTH_VERTIAL;line++){
				canvas.drawText(questionString.substring(CanvasSetting.QUESTION_WRAP_LENGTH_VERTIAL*line,CanvasSetting.QUESTION_WRAP_LENGTH_VERTIAL*(line+1)),queX,queY/4+lineHeight,mPaint);
				Log.i(LOG_TAG, "start:"+CanvasSetting.QUESTION_WRAP_LENGTH_VERTIAL*line+";end:"+CanvasSetting.QUESTION_WRAP_LENGTH_VERTIAL*(line+1));
				lineHeight+= queY/4;
			}
			Log.i(LOG_TAG, "start:"+CanvasSetting.QUESTION_WRAP_LENGTH_VERTIAL*line);
			canvas.drawText(questionString.substring(line*CanvasSetting.QUESTION_WRAP_LENGTH_VERTIAL),queX,queY/4+lineHeight,mPaint);
		}else{
			Log.i(LOG_TAG, "draw question text->queLength<="+CanvasSetting.QUESTION_WRAP_LENGTH_VERTIAL);
			canvas.drawText(questionString,queX,queY/2,mPaint);
		}
		canvas.save();
		//--------------------- draw question end -------------------
		
		
		//---------------------draw bar list start-------------------
		int canvasNoMarginWidth = canvasWidth - setting.getLeftMargin() - setting.getRightMargin();
		double barWidthInclSpace = (canvasNoMarginWidth * 1.0)/baseBean.getTotalCount();
		if(baseBean.getTotalPages()>1){
			barWidthInclSpace = (canvasNoMarginWidth * 1.0)/CHOICE_COUNT_PER_PAGE;
		}
		
		int barWidth = (int)(barWidthInclSpace*0.9);
		int spaceWidth = (int)(barWidthInclSpace*0.1);
		if(spaceWidth<1) spaceWidth = 1;
		Log.i(LOG_TAG, "barWidth:"+barWidth);
		Log.i(LOG_TAG, "spaceWidth:"+spaceWidth);
		
		setting.setSpace(spaceWidth);
		
		int canvasNoMarginHeight = canvasHeight-setting.getTopMargin()-setting.getBottomMargin();
		
		PageBaseBean pageBean = baseBean.getCurrentPageBaseBean(page);
		List<ChoiceResult> pageList = pageBean.getCrList();
		int pageListSize = pageList.size();
		if(pageListSize==0) return;
		
		for(int i=0;i<pageListSize;i++){
//			Bar2D bar2D = barList.get(i);
			Log.i(LOG_TAG, "begin loop:"+i+"----------------------------");
			
			ChoiceResult cr = pageList.get(i);
			
			int votes = cr.getCount();
			double votesShare = cr.getShare();
			
			Log.i(LOG_TAG, "votes:"+votes);
			Log.i(LOG_TAG, "votesShare:"+votesShare);
			
			int barHeigth = (int)((votesShare * canvasNoMarginHeight)*BAR_RATIO);
			Log.i(LOG_TAG, "barHeigth:"+barHeigth);
			
			int tlX = (int) (setting.getLeftMargin()+ i * (barWidth + setting.getSpace()));
			int tlY =  canvasHeight - setting.getBottomMargin() - barHeigth;
			int brX = tlX + barWidth;
			int brY = canvasHeight - setting.getBottomMargin();
			
			//draw bar
			mPaint.setColor(Color.parseColor(Configuration.CHART_COLOR_ARR[i]));
			canvas.drawRect(tlX,tlY,brX,brY, mPaint);
			canvas.save();
			
			//draw choice text
			mPaint.setTextSize(setting.getChoiceTextSize());
			mPaint.setColor(Color.parseColor(Configuration.CHART_CHOICE_TEXT_COLOR));
			Log.i(LOG_TAG, "draw:"+cr.getIndex()+" at ("+tlX+","+String.valueOf(canvasHeight-setting.getBottomMargin()/2)+")");
			canvas.drawText(String.valueOf(cr.getIndex()), tlX, canvasHeight-setting.getBottomMargin()/2, mPaint);
			canvas.save();
			
			//draw votes and share
			mPaint.setTextSize(setting.getAnswerTextSize());
			mPaint.setColor(Color.parseColor(Configuration.CHART_ANSWER_TEXT_COLOR));
			
			//answerTextStyle = 1 show votes;
			//answerTextStyle = 2 show share;
			//answerTextStyle = 3 show votes and share;
			String votesStr = String.valueOf(votes);
			String shareStr = "0%";
			if(votesShare*100>0){
				shareStr = NumberUtil.formatData0Deci(votesShare*100)+"%";
			}
			int answerTextStyle = baseBean.getAnswerTextStyle();
//			if("42".equals(type)){
//				answerTextStyle= 1;
//			}else if("45".equals(type)){
//				answerTextStyle= 1;
//			}else{
//				answerTextStyle = answer.getAnswerTextStyle();
//			}
			
			Log.i(LOG_TAG, "answerTextStyle:"+answerTextStyle);
			Log.i(LOG_TAG, "draw votes at("+tlX+","+String.valueOf(tlY-setting.getTextLineHeight()*2)+")");
			Log.i(LOG_TAG, "draw share at("+tlX+","+String.valueOf(tlY-setting.getTextLineHeight())+")");
			
			switch (answerTextStyle){
			case 1:canvas.drawText(votesStr, tlX, tlY-setting.getTextLineHeight()*2, mPaint);break;
			case 2:canvas.drawText(shareStr, tlX, tlY-setting.getTextLineHeight(), mPaint);break;
			case 3:
				canvas.drawText(votesStr, tlX, tlY-setting.getTextLineHeight()*2, mPaint);
				canvas.drawText(shareStr, tlX, tlY-setting.getTextLineHeight(), mPaint);
			}
			
			canvas.save();
		}
		
		//draw grid
//		mPaint.setColor(Color.WHITE);
//		for(int x=1;x<10;x++){
//			//draw horizontal lines
//			canvas.drawLine(leftM,x* canvasHeight/10, canvasWidth-rightM,x*canvasHeight/10, mPaint);
//		}
//		for(int y=1;y<10;y++){
//			//draw vertical lines
//			canvas.drawLine(y* canvasWidth/10,topM, y* canvasWidth/10, canvasHeight-bottomM, mPaint);
//		}
		
		//---------------------draw bar list end-------------------
		
		//draw page bullets list
		if(baseBean.getTotalPages()>1){
			int pageX = setting.getLeftMargin();
			for(int p=0;p<baseBean.getTotalPages();p++){
				mPaint.setColor(Color.GRAY);
				if(p+1 == page){
					mPaint.setColor(Color.WHITE);
				}
				canvas.drawCircle(pageX, canvasHeight-setting.getBottomMargin()/4, setting.getBulletRadius(), mPaint);
				pageX+=setting.getBulletRadius()*3;
			}
			canvas.save();
		}
		//end
		
		canvas.restore();
		sfh.unlockCanvasAndPost(canvas);
	}

	
}
